<?php

/**
 * 公告管理 
 *
 */ 

class noticeAction extends backendAction
{
    public function _initialize() {
        parent::_initialize();
        $this->_mod = D('notice');
    }

    public function _before_index() {
        $this->readyData();
    }

    protected function _search() {
        $map = D('notice')->noticeWhere();
        ($time_start = $this->_request('time_start', 'trim')) && $map['publishTime'][] = array('egt', strtotime($time_start));
        ($time_end = $this->_request('time_end', 'trim')) && $map['publishTime'][] = array('elt', strtotime($time_end)+(24*60*60-1));
        ($status = $this->_request('status', 'trim')) && $map['status'] = $status;        
        ($title = $this->_request('title', 'trim')) && $map['title'] = array('like', '%'.$title.'%');  
        //($region = $this->_request('region', 'intval')) && $map['region'] = $region;    
        ($region = $this->_request('region', 'intval')) && $map['_string'] =" FIND_IN_SET({$region},regionId) ";    
        $this->assign('search', array(
            'time_start' => $time_start,
            'time_end' => $time_end,
            'status'  => $status,
            'keyword' => $keyword,
            'region'=>$region,
        ));
        return $map;
    }

    public function _before_add()
    {

        $this->readyData();      
    }

    protected function _before_insert($data) {

        //上传附件
        if (!empty($_FILES['file']['name'])) {
            $art_add_time = date('ym/d/');
            $result = $this->_upload($_FILES['file'], C('NOTICE_ATT_DIR').'/' . $art_add_time,'','uniqid',C('NOTICE_UPLOADE_FILES'));
            if ($result['error']) {
                $this->error($result['info']);
            } else {
                $ext = array_pop(explode('.', $result['info'][0]['savename']));
                $data['att'] = $art_add_time . str_replace('.' . $ext, $ext, $result['info'][0]['savename']);
            }
        }

    

        $data['publishTime'] = strtotime($data['publishTime']);
        $data['addUserId']   = getAdminId();
        $data['editTime'] = time();
        $data['editUserId']   = getAdminId();
        
        return $data;
    }

    public function _before_edit(){
        $id = $this->_get('id','intval');
        $this->readyData(); 
    }
    
    
    /**
     * 准备数据，
     * 1、所有城市列表
     */ 
    private function readyData(){
        //城市列表
        $admin = getUserSession();
        $where = array(
            'level'=>1,
            'status'=>1
        );
        If(!empty($admin['regionIds'])){
            $where['id']= array('in',$admin['regionIds']);
        }
        $regionList = M('region')->where($where)->getField('id,name');       
        $this->assign('regionList',$regionList);
        
        if(IS_POST){
            if(is_array($_POST['region']) && !empty($_POST['region'])){
                $_POST['regionId'] = implode(',',$_POST['region']);
            }            
        }        
        
    }

    protected function _before_update($data) {
        if (!empty($_FILES['file']['name'])) {
            $art_add_time = date('ym/d/');
            //删除原
            $oldAtt = json_decode( $this->_mod->where(array('id'=>$data['id']))->getField('att'), true);
            $oldAtt = C('attach_path').C('NOTICE_ATT_DIR'). $oldAtt['savename'];
            is_file($oldAtt) && @unlink($oldAtt);
            //上传新附件  
            $result = $this->_upload($_FILES['file'], C('NOTICE_ATT_DIR').'/' . $art_add_time,'','uniqid',C('NOTICE_UPLOADE_FILES'));
            if ($result['error']) {
                $this->error($result['info']);
            } else {
                $data['att'] = json_encode(array('name'=>$result['info'][0]['name'],'savename'=>$art_add_time.$result['info'][0]['savename']));
            }
        } else {
            unset($data['att']);
        }
        if(is_array($data['region'])){
            $data['region'] = implode(',',$data['region']);
        }        
        $data['publishTime'] = strtotime($data['publishTime']);
        $data['addUserId']   = getAdminId();
        $data['editTime'] = time();
        $data['editUserId']   = getAdminId();
        return $data;
    }
    
    public function show(){
        $id = $this->_request('id','intval');
        $notice = M('notice')->where(array('id'=>$id))->find();
        $this->assign('info',$notice);
        $this->display();
    }


}

?>